package com.fast.config;

import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

/**
 * @author hujunwei
 * @Description: swagger文档配置类
 * @Date: 2020/11/23
 * @EnableSwaggerBootstrapUI: 第三方swagger增强API注解，不加这个会出现前端接口页面无法正常显示
 */
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
    @Value("${swagger.is.enable}")
    private boolean swaggerIsEnable;

    /*引入Knife4j提供的扩展类,配合配置文件中的knife4j.enable: true开启增强模式，支持接口分组*/
    private OpenApiExtensionResolver openApiExtensionResolver;

    @Autowired
    public void SwaggerConfiguration(OpenApiExtensionResolver openApiExtensionResolver) {
        this.openApiExtensionResolver = openApiExtensionResolver;
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact(" 胡俊伟", " http:localhost:80", "1149088509@qq.com");
        return new ApiInfoBuilder()
                .title("FastBoot快速搭建项目平台")
                .description("为了快速搭建项目而生，节省开发周期，降低搭建成本")
                .contact(contact)
                .termsOfServiceUrl("http://www.fastboot.com")
                .version(" 1.0.0")
                .build();
    }

    @Bean(value = "defaultApi1")
    public Docket defaultApi1() {
        String name = "API接口";
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(swaggerIsEnable)
                //分组名称
                .groupName(name)
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.fast.controller"))
                .paths(PathSelectors.any())
                .build()
                .extensions(openApiExtensionResolver.buildExtensions(name));
        return docket;
    }

    //如果一个项目中涉及到多个模块,可以对接口进行分组管理
//    @Bean
//    public Docket createRestApi2() {
//        return new Docket(DocumentationType.SWAGGER_2)
//                .enable(swaggerIsEnable)
//                .groupName("二级网关模块接口")
//                .apiInfo(apiInfo())
//                .host("http:localhost:80")
//                .select()
//                .apis(RequestHandlerSelectors.basePackage("com.jiulong.springboot_validator.controller2"))
//                .paths(PathSelectors.any())
//                .build();
//    }

}
